﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FantasyDraft2.Models;

namespace FantasyDraft2.Controllers
{
    public class TradeController : Controller
    {
        FantasyDraftEntities fantasyDB = new FantasyDraftEntities();
        //
        // GET: /Trade/

        public ActionResult Index()
        {

            TeamTradeViewModel model = new TeamTradeViewModel
            {
                TeamNames = fantasyDB.Users.ToList(),
                TeamOneId = 0,
                TeamTwoId = 0
            };
            return View(model);
        }

        //
        // POST: /Trade/DoTeams
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(TeamTradeViewModel model)
        {
            IEnumerable<Player> PlayersTeamOne = fantasyDB.Players.Where(p => p.TeamId == model.TeamOneId);
            IEnumerable<Player> PlayersTeamTwo = fantasyDB.Players.Where(p => p.TeamId == model.TeamTwoId);

            PlayerTrade newModel = new PlayerTrade
            {
                TeamOnePlayers = PlayersTeamOne,
                TeamTwoPlayers = PlayersTeamTwo,
                Player1Id = 0,
                Player2Id = 0,
                Team1Id = model.TeamOneId,
                Team2Id = model.TeamTwoId
            };
            TempData["Model"] = newModel;
            return RedirectToAction("PlayerTrade");
        }

        //
        // GET: /PlayerTrade/
        public ActionResult PlayerTrade()
        {
            Object c = TempData["Model"];
            PlayerTrade newModel = (PlayerTrade)c;
            return View(newModel);
        }

        //
        // POST: /PlayerTrade/
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult PlayerTrade(PlayerTrade tradedPlayers)
        {
            PlayerEvent playerOne = new PlayerEvent{
                EventType = 1,
                InitialTeamId = tradedPlayers.Team1Id,
                FinalTeamId = tradedPlayers.Team2Id,
                PlayerId = tradedPlayers.Player1Id,
                EventDate = DateTime.Now
            };
            fantasyDB.PlayerEvents.Add(playerOne);


            PlayerEvent playerTwo = new PlayerEvent{
                EventType = 1,
                InitialTeamId = tradedPlayers.Team2Id,
                FinalTeamId = tradedPlayers.Team1Id,
                PlayerId = tradedPlayers.Player2Id,
                EventDate = DateTime.Now
            };
            fantasyDB.PlayerEvents.Add(playerTwo);

            //Test
            //Change Player1's TeamId to traded team.
            var player1 = fantasyDB.Players.Find(tradedPlayers.Player1Id);
            player1.TeamId = tradedPlayers.Team2Id;
            fantasyDB.Entry<Player>(player1).State = System.Data.EntityState.Modified; 
            //Change Player2's TeamId to traded team.
            var player2 = fantasyDB.Players.Find(tradedPlayers.Player2Id);
            player2.TeamId = tradedPlayers.Team1Id;
            fantasyDB.Entry<Player>(player2).State = System.Data.EntityState.Modified;


            fantasyDB.SaveChanges();
            return RedirectToAction("Index", "Admin");
        }

    }
}